package com.itmk.web.sys_banner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.annotation.Auth;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.goods.entity.Goods;
import com.itmk.web.goods.service.GoodsService;
import com.itmk.web.goods_category.entity.SelectType;
import com.itmk.web.sys_banner.entity.BannerParm;
import com.itmk.web.sys_banner.entity.SysBanner;
import com.itmk.web.sys_banner.service.SysBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className SysBannerController.java
 * @date 2023 12 05
 */
@RestController
@RequestMapping("/api/banner")
public class SysBannerController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    private SysBannerService sysBannerService;
    @Auth
    @CrossOrigin
    @PostMapping()
    public ResultVo add(@RequestBody SysBanner sysBanner){
        sysBanner.setBanId(sysBannerService.maxId());
        if (sysBannerService.save(sysBanner)){
            return ResultUtils.success("添加了一个广告");
        }
        return ResultUtils.error("没添加上");
    }
    @Auth
    @CrossOrigin
    @PutMapping
    public ResultVo edit(@RequestBody SysBanner sysBanner){
        if (sysBannerService.updateById(sysBanner)){
            return ResultUtils.success("yes");
        }
        return ResultUtils.error("No");
    }

    @Auth
    @CrossOrigin
    @DeleteMapping("/{banId}")
    public ResultVo delete(@PathVariable("banId") Long banId){
        if(sysBannerService.removeById(banId)){
            return ResultUtils.success("yes");
        }
        return ResultUtils.error("No");
    }

    @Auth
    @CrossOrigin
    @GetMapping("/list")
    public ResultVo list(BannerParm  parm){
//构造分页对象
        IPage<SysBanner> page = new Page<>
                (parm.getCurrentPage(),parm.getPageSize());
//构造查询条件
        QueryWrapper<SysBanner> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getTitle()),SysBanner::getTitle
                        ,parm.getTitle())
                .orderByDesc(SysBanner::getOrderNum);
        IPage<SysBanner> list = sysBannerService.page(page, query);
        return ResultUtils.success("查询成功!",list);
    }

    //商品列表
    @Auth
    @CrossOrigin
    @GetMapping("/getGoods")
    public ResultVo getGoods(){
//查询商品
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().eq(Goods::getStatus,"0").eq(Goods::getSellStatus,"0");
        List<Goods> list = goodsService.list(query);
//组装select需要的数据格式
        List<SelectType> selectList = new ArrayList<>();
        for (Goods goods : list) {
            SelectType type = new SelectType();
            type.setValue(goods.getGoodsId());
            type.setLabel(goods.getGoodsName());
            selectList.add(type);
        }
        return ResultUtils.success("查询成功",selectList);
    }

    //上下架广告
    @Auth
    @CrossOrigin
    @PostMapping("/upandown")
    public ResultVo upandown(@RequestBody SysBanner sysBanner){
       LambdaUpdateWrapper<SysBanner> wrapper = new LambdaUpdateWrapper<>();
       wrapper.set(SysBanner::getStatus,sysBanner.getStatus())
               .eq(SysBanner::getBanId,sysBanner.getBanId());
       if (sysBannerService.update(wrapper)){
           return ResultUtils.success("yes");
       }
       return ResultUtils.error("error");
    }

    //查询小程序首页轮播图数据
    @GetMapping("/getIndexBanner")
    public ResultVo getIndexBanner(){
        QueryWrapper<SysBanner> query = new QueryWrapper<>();
        query.lambda().eq(SysBanner::getStatus,"0")
                .orderByAsc(SysBanner::getOrderNum);
        List<SysBanner> list = sysBannerService.list(query);
        if(list.size() > 0){
            for (SysBanner sysBanner : list) {
                Long goodsId = sysBanner.getGoodsId();
                Goods goods = goodsService.getById(goodsId);
                sysBanner.setGoods(goods);
            }
        }
        return ResultUtils.success("查询成功",list);
    }
}